import com.ailk.kbi.kbirazor.control.AreaControl;
import com.ailk.kbi.kbirazor.control.BrandControl;
import com.ailk.kbi.kbirazor.model.AreaModel;
import com.ailk.kbi.kbirazor.model.BrandModel;
import com.ailk.kbi.kbirazor.model.TimeModel;
import com.ailk.kbi.kbirazor.vo.DimVO;
import com.ailk.kbi.kbirazor.vo.KpiVO;

import flash.events.Event;

import mx.charts.series.ColumnSeries;
import mx.charts.series.LineSeries;
import mx.collections.XMLListCollection;
import mx.controls.AdvancedDataGrid;
import mx.controls.Alert;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.core.IFlexDisplayObject;
import mx.events.DragEvent;
import mx.events.FlexEvent;
import mx.managers.DragManager;
import mx.managers.PopUpManager;



[Bindable]
private var brandModel:BrandModel = BrandModel.getInstance();

private var brandControl:BrandControl = new BrandControl();


/**表格列的数据*/
private var columns:Array = new Array();

/**存储图表的序列数组*/
private var series:Array = new Array();

/**初始化窗口*/
protected function creationCompleteHandler(event:FlexEvent):void {
	var dgc:AdvancedDataGridColumn = new AdvancedDataGridColumn();
	dgc.headerText = "品牌";
	dgc.width = 130;
	dgc.minWidth = 130;
	dgc.dataField = "@brandName";
	columns.push(dgc);
	brandGrid.columns = columns;
	brandChart.series = series;
}
/**关闭窗口*/
private function closeHandler(event:Event):void {
	event.target.removeEventListener("close",closeHandler);
	PopUpManager.removePopUp(event.target as IFlexDisplayObject);
	brandModel.windowFlag = false;
	brandModel.dataXML = <result></result>;
	brandGrid.columns = null;
	
	columns = new Array();
	series = new Array();
	brandModel.kpiVOList.removeAll();
	brandModel.kpiIdList.removeAll();
	
}

private function dragEnterHandler(event:DragEvent):void {
	if (event.dragSource.hasFormat('KpiVO')) { 
		var dropTarget:AdvancedDataGrid=AdvancedDataGrid(event.currentTarget); 
		DragManager.acceptDragDrop(dropTarget); 
	} 
}
private function dragDropHandler(event:DragEvent):void {
	var kpiVO:KpiVO = event.dragSource.dataForFormat('KpiVO') as KpiVO;
	
	if(kpiVO.brandFlag == 1) {
		
		
		
		for each(var kpiVONow:KpiVO in brandModel.kpiVOList) {
			if(kpiVO.kpiId == kpiVONow.kpiId) {
				Alert.show("该项指标已经存在！","提示");
				return;
			}
		}
		
		brandModel.kpiVOList.addItem(kpiVO);
		brandModel.kpiIdList.addItem(kpiVO.kpiId);
		brandModel.opTime = kpiVO.opTime;
		
		brandControl.getBrandDataList();
		
		//	areaModel.dataXML.data.@cityName = kpiVO.cityName;
		//	areaModel.dataXML.data.@["kpiValue_"+kpiVO.kpiId] = kpiVO.kpiValue;
		//	areaModel.dataXML.data.@["kpiId_"+kpiVO.kpiId] = kpiVO.kpiId;
		//	areaModel.dataXML.data.@cityId = kpiVO.cityId;
		//	areaModel.dataXML.data.@areaId = kpiVO.areaId;
		//	areaModel.dataXML.data.@userId = kpiVO.userId;
		//	areaModel.dataXML.data.@opTime = kpiVO.opTime;
		
		
		//	if(areaModel.dataXML.data.children().length() > 0) {
		//		var xmlCollection:XMLListCollection = new XMLListCollection(areaModel.dataXML.data.children());
		//		xmlCollection.removeAll();
		//	}
		
		/*更新表格*/
		var dgc:AdvancedDataGridColumn = new AdvancedDataGridColumn();
		with(dgc){
			headerText = kpiVO.kpiName;
			dataField = "@kpiValue_" + kpiVO.kpiId;
			width = 100;
			minWidth = 100;
		}
		columns.push(dgc);
		brandGrid.columns = columns;
		
		
		/*更新图表*/
		var newSeries:ColumnSeries = new ColumnSeries();
		with(newSeries){
			displayName = dgc.headerText;
			yField = dgc.dataField;
		}
		series.push(newSeries);	
		brandChart.series = series;
		
	} else {
		Alert.show("该指标无品牌信息","提示");
	}
}

/**下钻*/
//private function viewDetail():void {
//	//		Alert.show("kpi个数：" + kbrModel.areaWindowTitleArr.length);
//	if(areaGrid.selectedIndex > -1){
//		var selectedXML:XML = areaModel.selectedXML = areaGrid.selectedItem as XML;
//		if(selectedXML.children().length()==0){
//			Alert.show(areaGrid.selectedItem.@areaId);
//			var dimVO:DimVO = new DimVO();
//			
//			dimVO.areaId = selectedXML.@areaId;
//			dimVO.cityId = selectedXML.@cityId;
//			dimVO.opTime = selectedXML.@opTime;
//			
//			areaModel.dimVO = dimVO;		
//			
//			if(dimVO.cityId=='999'){//全市
//				areaControl.cityList();
//			}
//			//			else if(dimVO.areaId=='999'){//分公司
//			//				overviewKpiControl.countylist();
//			//			}
//			//				//dimVO.kpiId && dimVO.cityId && dimVO.countyId && 
//			
//			//			dimVO.areaId &&
//			//			else if(dimVO.schoolId=="9999"){
//			//				overviewKpiControl.schoollist();
//			//			}
//		}		
//	}
//}